package com.mall.controller;

import com.mall.domain.*;

import com.mall.service.impl.CarServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/car")
public class CarController {
    @Autowired
    private CarServiceImpl carService;

    /**
     * 查询用户
     * @param
     * @return
     */
    @RequestMapping("/findAll")
    public @ResponseBody
    Map<String, Object> findAll(Integer members_id){
        Map<String,Object> car = new HashMap<>();
        List<Car> cars = carService.findAll(members_id);
        car.put("cars",cars);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        metaMap.put("msg","获取订单成功");
        metaMap.put("status",200);
        car.put("meta",metaMap);
        return car;
    }



    /**
     * 添加用户
     * @param car
     * @return
     */
    @RequestMapping("/add")
    public @ResponseBody
    Map<String, Object> add(@RequestBody Car car ){

        Map<String,Object> carMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        //放入dataMap集合
        dataMap.put("cars",car);
        //放入Map集合
        carMap.put("data",dataMap);
        //创建状态Map

        Map<String,Object> metaMap = new HashMap<>();
        int flag = carService.add(car);
        if(flag > 0 ){
            metaMap.put("msg","添加购物车成功");
            metaMap.put("status",200);
        } else {
            metaMap.put("msg","添加购物车失败");
            metaMap.put("status",0);
        }
        carMap.put("meta",metaMap);
        return carMap;
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @RequestMapping("/del")
    public @ResponseBody
    Map<String, Object> add(Integer id){
        Map<String,Object> carMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        //放入Map集合
        carMap.put("data",dataMap);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        int flag = carService.del(id);
        if(flag > 0){
            metaMap.put("msg","删除订单成功");
            metaMap.put("status",200);
        } else {
            metaMap.put("msg","删除订单失败");
            metaMap.put("status",0);
        }
        carMap.put("meta",metaMap);
        return carMap;
    }

    /**
     * 更新购物车
     * @param
     * @return
     */
    @RequestMapping("/update")
    public @ResponseBody
        Map<String, Object> update(@RequestBody Car car){
        Map<String,Object> roleMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        //放入dataMap集合
        dataMap.put("car",car);

        //放入Map集合
        roleMap.put("data",dataMap);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        int flag = carService.update(car);
        if(flag > 0){
            metaMap.put("msg","该订单已完成");
            metaMap.put("status",200);
        } else {
            metaMap.put("msg","更新订单失败");
            metaMap.put("status",0);
        }
        roleMap.put("meta",metaMap);
        return roleMap;
    }
    /**
     * 更新购物车
     * @param
     * @return
     */
    @PostMapping("/update1")
    public @ResponseBody Map<String, Object> update1(@RequestBody Car car){

        Map<String,Object> roleMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        //放入dataMap集合
        dataMap.put("car",car);

        //放入Map集合
        roleMap.put("data",dataMap);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        int flag = carService.update1(car);
        if(flag > 0){
            metaMap.put("msg","付款成功");
            metaMap.put("status",200);
        } else {
            metaMap.put("msg","付款失败");
            metaMap.put("status",0);
        }
        roleMap.put("meta",metaMap);
        return roleMap;
    }
    /**
     * 查询所有订单
     * @param page
     * @return
     */
    @RequestMapping("/findAlls")
    public @ResponseBody
    Map<String, Object> findAll1(@RequestBody Page page){
        Map<String,Object> orderMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        //获取订单集合
        List<Car> cars = carService.findAll1(page);
        //放入dataMap集合
        dataMap.put("cars",cars);
        dataMap.put("total",carService.findTotal());
        //放入Map集合
        orderMap.put("data",dataMap);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        metaMap.put("msg","获取订单列表成功");
        metaMap.put("status",200);

        orderMap.put("meta",metaMap);
        return orderMap;
    }

}
